package org.xi.structural.flyweight.factory;

import org.xi.structural.flyweight.api.Icon;
import org.xi.structural.flyweight.impl.ConcreteIcon;

import java.util.HashMap;
import java.util.Map;

/**
 * 享元工厂
 */
public class IconFactory {
    private final Map<String, ConcreteIcon> iconPool = new HashMap<>();

    public Icon getIcon(String shape) {
        ConcreteIcon icon = iconPool.get(shape);
        if (icon == null) {
            icon = new ConcreteIcon(shape);
            iconPool.put(shape, icon);
            System.out.printf("Creating and caching icon of shape '%s'%n", shape);
        }
        return icon;
    }
}